package com.tebiecloud.store.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.tebiecloud.common.utils.RedisUtils;
import com.tebiecloud.common.utils.UUIDUtil;
import com.tebiecloud.store.client.model.entity.StoreStaffWallet;
import com.tebiecloud.store.client.model.vo.DeductAmountVo;
import com.tebiecloud.store.client.model.vo.PlusAmountVo;
import com.tebiecloud.store.server.mapper.StoreStaffWalletMapper;
import com.tebiecloud.store.server.service.StoreStaffWalletService;
import com.tebiecloud.common.mybatis.base.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 *  服务实现类
 * @author tebie
 * @date 2019-09-15
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class StoreStaffWalletServiceImpl extends BaseServiceImpl<StoreStaffWalletMapper, StoreStaffWallet> implements StoreStaffWalletService {


    @Autowired
    StoreStaffWalletMapper storeStaffWalletMapper;

    @Autowired
    RedisUtils redisUtils;

    @Override
    public DeductAmountVo updateDeductAmount(Long storeStaffId, int amount, int maxCoin) {
        DeductAmountVo deductAmountVo = new DeductAmountVo();
        deductAmountVo.setDeductBalance(0);
        deductAmountVo.setDeductCoin(0);
        int time = 10;
        String key = String.valueOf(storeStaffId);
        Boolean isLock = redisUtils.lock(key,time);
        if(!isLock){
            throw new RuntimeException("资金池被占用，请稍等");
        }
        //查询钱包
        QueryWrapper<StoreStaffWallet> storeStaffWalletQueryWrapper = new QueryWrapper<>();
        storeStaffWalletQueryWrapper.eq("store_staff_id",storeStaffId);
        StoreStaffWallet storeStaffWallet = storeStaffWalletMapper.selectOne(storeStaffWalletQueryWrapper);
        //初始化默认是负数，负数不给更新
        int currentAmount = -1;
        int currentCoin = -1;
        if(storeStaffWallet != null){
            currentAmount = storeStaffWallet.getBalance() - amount;
            currentCoin = storeStaffWallet.getCoin() - maxCoin;
            if(currentAmount>=0){
                deductAmountVo.setDeductBalance(amount);
            }else {
                deductAmountVo.setDeductBalance(0);
            }
            if(currentCoin>=0){
                deductAmountVo.setDeductCoin(maxCoin);
            }else{
                deductAmountVo.setDeductCoin(0);
            }
            //更新数据
            StoreStaffWallet storeStaffWallet1 = new StoreStaffWallet();
            UpdateWrapper<StoreStaffWallet> storeStaffWalletUpdateWrapper = new UpdateWrapper<>();
            storeStaffWalletUpdateWrapper.eq("store_staff_id",storeStaffId);
            if(currentAmount>=0)storeStaffWallet1.setBalance(currentAmount);
            if(currentCoin>=0)storeStaffWallet1.setCoin(currentCoin);
            storeStaffWalletMapper.update(storeStaffWallet1,storeStaffWalletUpdateWrapper);
        }
        //解锁
        redisUtils.unLock(key);
        return deductAmountVo;
    }

    @Override
    public PlusAmountVo updatePlusAmount(Long storeStaffId, int balance, int coin) {
        PlusAmountVo plusAmountVo = new PlusAmountVo();
        plusAmountVo.setPlusBalance(0);
        plusAmountVo.setPlusCoin(0);
        int time = 10;
        String key = String.valueOf(storeStaffId);
        Boolean isLock = redisUtils.lock(key,time);
        if(!isLock){
            throw new RuntimeException("资金池被占用，请稍等");
        }
        //查询钱包
        QueryWrapper<StoreStaffWallet> storeStaffWalletQueryWrapper = new QueryWrapper<>();
        storeStaffWalletQueryWrapper.eq("store_staff_id",storeStaffId);
        StoreStaffWallet storeStaffWallet = storeStaffWalletMapper.selectOne(storeStaffWalletQueryWrapper);
        if(storeStaffWallet != null){
            int currentAmount = storeStaffWallet.getBalance() + balance;
            int currentCoin = storeStaffWallet.getCoin() + coin;
            plusAmountVo.setPlusBalance(balance);
            plusAmountVo.setPlusCoin(coin);
            //更新数据
            StoreStaffWallet storeStaffWallet1 = new StoreStaffWallet();
            UpdateWrapper<StoreStaffWallet> storeStaffWalletUpdateWrapper = new UpdateWrapper<>();
            storeStaffWalletUpdateWrapper.eq("store_staff_id",storeStaffId);
            storeStaffWallet1.setBalance(currentAmount);
            storeStaffWallet1.setCoin(currentCoin);
            storeStaffWalletMapper.update(storeStaffWallet1,storeStaffWalletUpdateWrapper);
        }
        //解锁
        redisUtils.unLock(key);
        return plusAmountVo;
    }
}
